package ycj.learn.jvm;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
*@author : lihan
*@description : 测试内存溢出类
*@date : 2020/3/2 15:24
*/
public class OutOfMemoryTest {


    //堆内存溢出
    @Test
    public void heapOOM() {

        List<Object> objects = new ArrayList<Object>();

        while (true) {
            objects.add(new Object());
        }
    }

    //虚拟机栈,本地方法栈溢出
    private int stackLength = 1;
    @Test
    public void javaVMStackSOF() {

        stackLength++;
        this.javaVMStackSOF();
    }

    //运行时常量池溢出,jdk1.7该循环会一直下去
    @Test
    public void runtimeConstantPoolOOM() {
        ArrayList<String> strings = new ArrayList<>();
        int i = 0;
        while (true) {
            strings.add(String.valueOf(++i).intern());
        }

    }

    //方法区内存溢出
    public void methodAreaOOM() {
        //使用cglib字节码技术,将更多的类加载到内存中
    }

    //本机内存直接溢出
    public void directOOM() {

    }




}
